﻿using IOP.Pulsar.Abstractions;
using IOP.Pulsar.ProductLineClients.TCP;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ClientTest
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("脉冲星测试客户端");
            IClientOptions options = new DefaultClientOption();
            options.Address = "192.168.43.203";
            options.Port = 6666;
            ProductLineTCPClient<StringContext> testClient = new ProductLineTCPClient<StringContext>(services =>
            {
                services.AddLogging();
                services.AddTransient<IProductLineEntry<StringContext>>(factory => new DefaultProductLineEntry(new DefaultProductLine<StringContext>()));
            }, builder =>
            {
                builder.Run(end =>
                {
                    Console.WriteLine(end.Body);
                    Console.WriteLine("请输入字符:");
                    return Task.CompletedTask;
                });
            });
            await testClient.ConnectAsync(options);
            while (true)
            {
                var text = Console.ReadLine();
                if(text == "D")
                {
                    testClient.Disconnect();
                    await testClient.ConnectAsync(options);
                }
                text += '\n';
                string sendStr = text;
                byte[] sendBytes = Encoding.UTF8.GetBytes(sendStr);
                bool c = testClient.IsConnected;
                if (!testClient.IsConnected) break;
                testClient.Send(sendBytes);
            }
        }
    }
}
